clear all
	set varabbrev off
	set more off
	set rmsg on, permanently
	ssc install zipsave 


	global rawdatadirectory "/afs/crc.nd.edu/group/LEO/infutor_master_READONLY"
	*NOTE: CRC location of raw Infutor files; this should never change.
	
	global mydirectory "/afs/crc.nd.edu/group/LEO/infutor_downes_working/extractrequests/LosAngeles_CA/"
	*NOTE: Personal/project folder withing CRC LEO folder; requires updating.
	
	
/*SETUP EXTRACT OPTIONS==============================================
	These options need to be manually edited for every extract.
===================================================================*/

	global filedate 1221
	*NOTE: MMYY
	
	local namefuzzymatch = 1
	*NOTE: Set = 1 if the extract will be used in to fuzzy match on name. Set = 0 o.w.
	
	local countymatch = 1
	*NOTE: Set = 1 if the extract should include only records from a specified county. Set = 0 o.w.
	
	if `countymatch' == 0 {
		
		local extractstate CO
		
	}
	
	if `countymatch' == 1 {
		
		local extractstate CA
		local extractcountyfips 037
		
	}
	*NOTE: Some common county FIPS codes for LEO:
		* King County, WA 			033
		* Santa Clara County, CA 	085
		* Cook County, IL			031
		* Harris County, TX			201
		* Los Angeles County, CA	037
	
	
	
	


/*PREPARE FOR CRC LOOP==============================================
	To run as a job array on CRC, we need to associate each state 
	file with a number (e.g., AL = 1, AK = 2, ..., WY = 52). Later,
	we will loop through these numbers using the CRC local variable,
	and each loop will be a separate job.
===================================================================*/


	local st_counter = 0
		
	foreach state_abbrev in "AL" "AK" "AZ" "AR" "CA" "CO" "CT" "DE" "DC" "FL" "GA" "HI" "ID" "IL" "IN" "IA" "KS" "KY" "LA" "ME" "MD" "MA" "MI" "MN" "MS" "MO" "MT" "NE" "NV" "NH" "NJ" "NM" "NY" "NC" "ND" "OH" "OK" "OR" "PA" "PR" "RI" "SC" "SD" "TN" "TX" "UT" "VT" "VA" "WA" "WV" "WI" "WY" {
			
		local st_counter = `st_counter' + 1
		local state_abbrev`st_counter' `state_abbrev'
		
	}
	
	local crc "`1'"

	
	
	
	
/*CLEANING INFUTOR===================================================
	Rename and label variables using info from the included codebook
	& drop variables that we cannot identify (labeled "internal use",
	null SSNs, etc.). Keep only obs from the locations/timeframe we 
	care about.
===================================================================*/

	di "`state_abbrev`crc''"
	di "`crc'"
	

***(1) Import, clean and save the first 30M obs:
	*NOTE: Sometimes Stata crashes when trying to import all obs for the big states, so to be safe it's best to break them up.
	
	cd "$rawdatadirectory"
	import delimited using CRD4_${filedate}_`state_abbrev`crc''.txt, stringcols(96 104 105 110 121 129 130 135 146 154 155 160 171 179 180 185 196 204 205 210 221 229 230 235 246 254 255 260 271 279 280 285 296 304 305 310 321 329 330 335) varnames(nonames) rowrange(:30000000) clear

	*Rename variables
	rename v1 PID_
	rename v6 First_Name_0
	rename v8 Last_Name_0
	rename v10 Gender_0
	rename v11 DOB
	rename v12 DeceasedCD
	rename v18 orig_file_date_0
	rename v19 last_active_date_0
	rename v94 complete_address1
	rename v96 add_num1
	rename v97 pre_direction1
	rename v98 street_name1
	rename v99 street_suffix1
	rename v100 post_direction1
	rename v101 apartment_name1
	rename v102 apartment_number1
	rename v103 city1
	rename v104 state1
	rename v105 zip1
	rename v106 zip_plus1
	rename v110 fips_county1
	rename v111 dwell_type1
	rename v113 effective_date1
	rename v116 orig_file_date1
	rename v117 last_verif_date1
	rename v119 complete_address2
	rename v121 add_num2
	rename v122 pre_direction2
	rename v123 street_name2
	rename v124 street_suffix2
	rename v125 post_direction2
	rename v126 apartment_name2
	rename v127 apartment_number2
	rename v128 city2
	rename v129 state2
	rename v130 zip2
	rename v131 zip_plus2
	rename v135 fips_county2
	rename v136 dwell_type2
	rename v138 effective_date2
	rename v141 orig_file_date2
	rename v142 last_verif_date2
	rename v144 complete_address3
	rename v146 add_num3
	rename v147 pre_direction3
	rename v148 street_name3
	rename v149 street_suffix3
	rename v150 post_direction3
	rename v151 apartment_name3
	rename v152 apartment_number3
	rename v153 city3
	rename v154 state3
	rename v155 zip3
	rename v156 zip_plus3
	rename v160 fips_county3
	rename v161 dwell_type3
	rename v163 effective_date3
	rename v166 orig_file_date3
	rename v167 last_verif_date3
	rename v169 complete_address4
	rename v171 add_num4
	rename v172 pre_direction4
	rename v173 street_name4
	rename v174 street_suffix4
	rename v175 post_direction4
	rename v176 apartment_name4
	rename v177 apartment_number4
	rename v178 city4
	rename v179 state4
	rename v180 zip4
	rename v181 zip_plus4
	rename v185 fips_county4
	rename v186 dwell_type4
	rename v188 effective_date4
	rename v191 orig_file_date4
	rename v192 last_verif_date4
	rename v194 complete_address5
	rename v196 add_num5
	rename v197 pre_direction5
	rename v198 street_name5
	rename v199 street_suffix5
	rename v200 post_direction5
	rename v201 apartment_name5
	rename v202 apartment_number5
	rename v203 city5
	rename v204 state5
	rename v205 zip5
	rename v206 zip_plus5
	rename v210 fips_county5
	rename v211 dwell_type5
	rename v213 effective_date5
	rename v216 orig_file_date5
	rename v217 last_verif_date5
	rename v219 complete_address6
	rename v221 add_num6
	rename v222 pre_direction6
	rename v223 street_name6
	rename v224 street_suffix6
	rename v225 post_direction6
	rename v226 apartment_name6
	rename v227 apartment_number6
	rename v228 city6
	rename v229 state6
	rename v230 zip6
	rename v231 zip_plus6
	rename v235 fips_county6
	rename v236 dwell_type6
	rename v238 effective_date6
	rename v241 orig_file_date6
	rename v242 last_verif_date6
	rename v244 complete_address7
	rename v246 add_num7
	rename v247 pre_direction7
	rename v248 street_name7
	rename v249 street_suffix7
	rename v250 post_direction7
	rename v251 apartment_name7
	rename v252 apartment_number7
	rename v253 city7
	rename v254 state7
	rename v255 zip7
	rename v256 zip_plus7
	rename v260 fips_county7
	rename v261 dwell_type7
	rename v263 effective_date7
	rename v266 orig_file_date7
	rename v267 last_verif_date7
	rename v269 complete_address8
	rename v271 add_num8
	rename v272 pre_direction8
	rename v273 street_name8
	rename v274 street_suffix8
	rename v275 post_direction8
	rename v276 apartment_name8
	rename v277 apartment_number8
	rename v278 city8
	rename v279 state8
	rename v280 zip8
	rename v281 zip_plus8
	rename v285 fips_county8
	rename v286 dwell_type8
	rename v288 effective_date8
	rename v291 orig_file_date8
	rename v292 last_verif_date8
	rename v294 complete_address9
	rename v296 add_num9
	rename v297 pre_direction9
	rename v298 street_name9
	rename v299 street_suffix9
	rename v300 post_direction9
	rename v301 apartment_name9
	rename v302 apartment_number9
	rename v303 city9
	rename v304 state9
	rename v305 zip9
	rename v306 zip_plus9
	rename v310 fips_county9
	rename v311 dwell_type9
	rename v313 effective_date9
	rename v316 orig_file_date9
	rename v317 last_verif_date9
	rename v319 complete_address10
	rename v321 add_num10
	rename v322 pre_direction10
	rename v323 street_name10
	rename v324 street_suffix10
	rename v325 post_direction10
	rename v326 apartment_name10
	rename v327 apartment_number10
	rename v328 city10
	rename v329 state10
	rename v330 zip10
	rename v331 zip_plus10
	rename v335 fips_county10
	rename v336 dwell_type10
	rename v338 effective_date10
	rename v341 orig_file_date10
	rename v342 last_verif_date10
	
	if `namefuzzymatch' == 1 {
	
		rename v25 First_Name_1
		rename v27 Last_Name_1
		rename v29 Gender_1
		rename v32 First_Name_2
		rename v34 Last_Name_2
		rename v36 Gender_2
		rename v39 First_Name_3
		rename v41 Last_Name_3
		rename v43 Gender_3
		rename v46 First_Name_4
		rename v48 Last_Name_4
		rename v50 Gender_4
		rename v53 First_Name_5
		rename v55 Last_Name_5
		rename v57 Gender_5
		rename v60 First_Name_6
		rename v62 Last_Name_6
		rename v64 Gender_6
		rename v67 First_Name_7
		rename v69 Last_Name_7
		rename v71 Gender_7
		rename v74 First_Name_8
		rename v76 Last_Name_8
		rename v78 Gender_8
		rename v81 First_Name_9
		rename v83 Last_Name_9
		rename v85 Gender_9
		rename v88 First_Name_10
		rename v90 Last_Name_10
		rename v92 Gender_10
	
	}

	*Label variables:
	label variable PID_ "Infutor ID"
	label variable First_Name_0 "Given Name"
	label variable Last_Name_0 "Surname"
	label variable Gender_0 "Gender (M,F,U)"
	label variable DOB "Date of Birth (YYYYMM)"
	label variable DeceasedCD "Deceased Flag Y means individual is deceased"
	  
	*Drop unnecessary variables:
	drop v*
		
	*Generate ever having lived in specified area:
	qui gen keep = 0
	forvalues x = 1(1)10 {
			
		if `countymatch' == 0 {
		
			qui replace keep = 1 if state`x' == "`extractstate'" 
		
		}
		
		if `countymatch' == 1 {
		
			qui replace keep = 1 if state`x' == "`extractstate'" & fips_county`x' == "`extractcountyfips'"
		
		}
				
	}
				
	*Keep only individuals who ever lived in specified area::
	keep if keep == 1
	drop keep
			
	*Clean some variables:
	forvalues x = 1(1)10 {
		
		tostring state`x', replace 
		destring zip`x', replace force
		destring effective_date`x', replace force
		destring last_verif_date`x', replace force
		
	}
	
	*Manipulating birth date for matching:
	*gen yob_inf = floor(DOB/100)
	*gen mob_inf = DOB - 100 * yob_inf
	
	*Note the state file:
	qui gen statefile = "`state_abbrev`crc''"
	
	*Condense file and save:
	compress
	
	cd "$mydirectory"
	zipsave `state_abbrev`crc''1_cleaned, replace
	
	
	
***(2) Import, clean and save next 30M obs:
	*NOTE: Sometimes Stata crashes when trying to import all obs for the big states, so to be safe it's best to break them up.
	
	cd "$rawdatadirectory"
	import delimited using CRD4_${filedate}_`state_abbrev`crc''.txt, stringcols(96 104 105 110 121 129 130 135 146 154 155 160 171 179 180 185 196 204 205 210 221 229 230 235 246 254 255 260 271 279 280 285 296 304 305 310 321 329 330 335) varnames(nonames) rowrange(30000001:60000000) clear
	
	if _N > 0 {

		*Rename variables
		rename v1 PID_
		rename v6 First_Name_0
		rename v8 Last_Name_0
		rename v10 Gender_0
		rename v11 DOB
		rename v12 DeceasedCD
		rename v18 orig_file_date_0
		rename v19 last_active_date_0
		rename v94 complete_address1
		rename v96 add_num1
		rename v97 pre_direction1
		rename v98 street_name1
		rename v99 street_suffix1
		rename v100 post_direction1
		rename v101 apartment_name1
		rename v102 apartment_number1
		rename v103 city1
		rename v104 state1
		rename v105 zip1
		rename v106 zip_plus1
		rename v110 fips_county1
		rename v111 dwell_type1
		rename v113 effective_date1
		rename v116 orig_file_date1
		rename v117 last_verif_date1
		rename v119 complete_address2
		rename v121 add_num2
		rename v122 pre_direction2
		rename v123 street_name2
		rename v124 street_suffix2
		rename v125 post_direction2
		rename v126 apartment_name2
		rename v127 apartment_number2
		rename v128 city2
		rename v129 state2
		rename v130 zip2
		rename v131 zip_plus2
		rename v135 fips_county2
		rename v136 dwell_type2
		rename v138 effective_date2
		rename v141 orig_file_date2
		rename v142 last_verif_date2
		rename v144 complete_address3
		rename v146 add_num3
		rename v147 pre_direction3
		rename v148 street_name3
		rename v149 street_suffix3
		rename v150 post_direction3
		rename v151 apartment_name3
		rename v152 apartment_number3
		rename v153 city3
		rename v154 state3
		rename v155 zip3
		rename v156 zip_plus3
		rename v160 fips_county3
		rename v161 dwell_type3
		rename v163 effective_date3
		rename v166 orig_file_date3
		rename v167 last_verif_date3
		rename v169 complete_address4
		rename v171 add_num4
		rename v172 pre_direction4
		rename v173 street_name4
		rename v174 street_suffix4
		rename v175 post_direction4
		rename v176 apartment_name4
		rename v177 apartment_number4
		rename v178 city4
		rename v179 state4
		rename v180 zip4
		rename v181 zip_plus4
		rename v185 fips_county4
		rename v186 dwell_type4
		rename v188 effective_date4
		rename v191 orig_file_date4
		rename v192 last_verif_date4
		rename v194 complete_address5
		rename v196 add_num5
		rename v197 pre_direction5
		rename v198 street_name5
		rename v199 street_suffix5
		rename v200 post_direction5
		rename v201 apartment_name5
		rename v202 apartment_number5
		rename v203 city5
		rename v204 state5
		rename v205 zip5
		rename v206 zip_plus5
		rename v210 fips_county5
		rename v211 dwell_type5
		rename v213 effective_date5
		rename v216 orig_file_date5
		rename v217 last_verif_date5
		rename v219 complete_address6
		rename v221 add_num6
		rename v222 pre_direction6
		rename v223 street_name6
		rename v224 street_suffix6
		rename v225 post_direction6
		rename v226 apartment_name6
		rename v227 apartment_number6
		rename v228 city6
		rename v229 state6
		rename v230 zip6
		rename v231 zip_plus6
		rename v235 fips_county6
		rename v236 dwell_type6
		rename v238 effective_date6
		rename v241 orig_file_date6
		rename v242 last_verif_date6
		rename v244 complete_address7
		rename v246 add_num7
		rename v247 pre_direction7
		rename v248 street_name7
		rename v249 street_suffix7
		rename v250 post_direction7
		rename v251 apartment_name7
		rename v252 apartment_number7
		rename v253 city7
		rename v254 state7
		rename v255 zip7
		rename v256 zip_plus7
		rename v260 fips_county7
		rename v261 dwell_type7
		rename v263 effective_date7
		rename v266 orig_file_date7
		rename v267 last_verif_date7
		rename v269 complete_address8
		rename v271 add_num8
		rename v272 pre_direction8
		rename v273 street_name8
		rename v274 street_suffix8
		rename v275 post_direction8
		rename v276 apartment_name8
		rename v277 apartment_number8
		rename v278 city8
		rename v279 state8
		rename v280 zip8
		rename v281 zip_plus8
		rename v285 fips_county8
		rename v286 dwell_type8
		rename v288 effective_date8
		rename v291 orig_file_date8
		rename v292 last_verif_date8
		rename v294 complete_address9
		rename v296 add_num9
		rename v297 pre_direction9
		rename v298 street_name9
		rename v299 street_suffix9
		rename v300 post_direction9
		rename v301 apartment_name9
		rename v302 apartment_number9
		rename v303 city9
		rename v304 state9
		rename v305 zip9
		rename v306 zip_plus9
		rename v310 fips_county9
		rename v311 dwell_type9
		rename v313 effective_date9
		rename v316 orig_file_date9
		rename v317 last_verif_date9
		rename v319 complete_address10
		rename v321 add_num10
		rename v322 pre_direction10
		rename v323 street_name10
		rename v324 street_suffix10
		rename v325 post_direction10
		rename v326 apartment_name10
		rename v327 apartment_number10
		rename v328 city10
		rename v329 state10
		rename v330 zip10
		rename v331 zip_plus10
		rename v335 fips_county10
		rename v336 dwell_type10
		rename v338 effective_date10
		rename v341 orig_file_date10
		rename v342 last_verif_date10
	
		
		if `namefuzzymatch' == 1 {
	
			rename v25 First_Name_1
			rename v27 Last_Name_1
			rename v29 Gender_1
			rename v32 First_Name_2
			rename v34 Last_Name_2
			rename v36 Gender_2
			rename v39 First_Name_3
			rename v41 Last_Name_3
			rename v43 Gender_3
			rename v46 First_Name_4
			rename v48 Last_Name_4
			rename v50 Gender_4
			rename v53 First_Name_5
			rename v55 Last_Name_5
			rename v57 Gender_5
			rename v60 First_Name_6
			rename v62 Last_Name_6
			rename v64 Gender_6
			rename v67 First_Name_7
			rename v69 Last_Name_7
			rename v71 Gender_7
			rename v74 First_Name_8
			rename v76 Last_Name_8
			rename v78 Gender_8
			rename v81 First_Name_9
			rename v83 Last_Name_9
			rename v85 Gender_9
			rename v88 First_Name_10
			rename v90 Last_Name_10
			rename v92 Gender_10
		
		}

		*Label variables:
		label variable PID_ "Infutor ID"
		label variable First_Name_0 "Given Name"
		label variable Last_Name_0 "Surname"
		label variable Gender_0 "Gender (M,F,U)"
		label variable DOB "Date of Birth (YYYYMM)"
		label variable DeceasedCD "Deceased Flag Y means individual is deceased"
		  
		*Drop unnecessary variables:
		drop v*
			
		*Generate ever having lived in specified area:
		qui gen keep = 0
		forvalues x = 1(1)10 {
				
			if `countymatch' == 0 {
			
				qui replace keep = 1 if state`x' == "`extractstate'" 
			
			}
			
			if `countymatch' == 1 {
			
				qui replace keep = 1 if state`x' == "`extractstate'" & fips_county`x' == "`extractcountyfips'"
			
			}
					
		}
					
		*Keep only individuals who ever lived in specified area::
		keep if keep == 1
			
		*Clean some variables:
		forvalues x = 1(1)10 {
			
			tostring state`x', replace 
			destring zip`x', replace force
			destring effective_date`x', replace force
			destring last_verif_date`x', replace force
		
		}
		
		*Manipulating birth date for matching:
		*gen yob_inf = floor(DOB/100)
		*gen mob_inf = DOB - 100 * yob_inf
		
		*Note the state file:
		qui gen statefile = "`state_abbrev`crc''"
		
		*Condense file and save:
		compress
		
		cd "$mydirectory"
		zipsave `state_abbrev`crc''2_cleaned, replace
		
	}
	
	if _N == 0 {
	    
		di "No obs"
		
	}
	
	
	
	
***(3) Import, clean and save last 30M obs:
	*NOTE: Sometimes Stata crashes when trying to import all obs for the big states, so to be safe it's best to break them up.
	
	cd "$rawdatadirectory"
	import delimited using CRD4_${filedate}_`state_abbrev`crc''.txt, stringcols(96 104 105 110 121 129 130 135 146 154 155 160 171 179 180 185 196 204 205 210 221 229 230 235 246 254 255 260 271 279 280 285 296 304 305 310 321 329 330 335) varnames(nonames) rowrange(60000001:) clear
	
	if _N > 0 {

		*Rename variables
		rename v1 PID_
		rename v6 First_Name_0
		rename v8 Last_Name_0
		rename v10 Gender_0
		rename v11 DOB
		rename v12 DeceasedCD
		rename v18 orig_file_date_0
		rename v19 last_active_date_0
		rename v94 complete_address1
		rename v96 add_num1
		rename v97 pre_direction1
		rename v98 street_name1
		rename v99 street_suffix1
		rename v100 post_direction1
		rename v101 apartment_name1
		rename v102 apartment_number1
		rename v103 city1
		rename v104 state1
		rename v105 zip1
		rename v106 zip_plus1
		rename v110 fips_county1
		rename v111 dwell_type1
		rename v113 effective_date1
		rename v116 orig_file_date1
		rename v117 last_verif_date1
		rename v119 complete_address2
		rename v121 add_num2
		rename v122 pre_direction2
		rename v123 street_name2
		rename v124 street_suffix2
		rename v125 post_direction2
		rename v126 apartment_name2
		rename v127 apartment_number2
		rename v128 city2
		rename v129 state2
		rename v130 zip2
		rename v131 zip_plus2
		rename v135 fips_county2
		rename v136 dwell_type2
		rename v138 effective_date2
		rename v141 orig_file_date2
		rename v142 last_verif_date2
		rename v144 complete_address3
		rename v146 add_num3
		rename v147 pre_direction3
		rename v148 street_name3
		rename v149 street_suffix3
		rename v150 post_direction3
		rename v151 apartment_name3
		rename v152 apartment_number3
		rename v153 city3
		rename v154 state3
		rename v155 zip3
		rename v156 zip_plus3
		rename v160 fips_county3
		rename v161 dwell_type3
		rename v163 effective_date3
		rename v166 orig_file_date3
		rename v167 last_verif_date3
		rename v169 complete_address4
		rename v171 add_num4
		rename v172 pre_direction4
		rename v173 street_name4
		rename v174 street_suffix4
		rename v175 post_direction4
		rename v176 apartment_name4
		rename v177 apartment_number4
		rename v178 city4
		rename v179 state4
		rename v180 zip4
		rename v181 zip_plus4
		rename v185 fips_county4
		rename v186 dwell_type4
		rename v188 effective_date4
		rename v191 orig_file_date4
		rename v192 last_verif_date4
		rename v194 complete_address5
		rename v196 add_num5
		rename v197 pre_direction5
		rename v198 street_name5
		rename v199 street_suffix5
		rename v200 post_direction5
		rename v201 apartment_name5
		rename v202 apartment_number5
		rename v203 city5
		rename v204 state5
		rename v205 zip5
		rename v206 zip_plus5
		rename v210 fips_county5
		rename v211 dwell_type5
		rename v213 effective_date5
		rename v216 orig_file_date5
		rename v217 last_verif_date5
		rename v219 complete_address6
		rename v221 add_num6
		rename v222 pre_direction6
		rename v223 street_name6
		rename v224 street_suffix6
		rename v225 post_direction6
		rename v226 apartment_name6
		rename v227 apartment_number6
		rename v228 city6
		rename v229 state6
		rename v230 zip6
		rename v231 zip_plus6
		rename v235 fips_county6
		rename v236 dwell_type6
		rename v238 effective_date6
		rename v241 orig_file_date6
		rename v242 last_verif_date6
		rename v244 complete_address7
		rename v246 add_num7
		rename v247 pre_direction7
		rename v248 street_name7
		rename v249 street_suffix7
		rename v250 post_direction7
		rename v251 apartment_name7
		rename v252 apartment_number7
		rename v253 city7
		rename v254 state7
		rename v255 zip7
		rename v256 zip_plus7
		rename v260 fips_county7
		rename v261 dwell_type7
		rename v263 effective_date7
		rename v266 orig_file_date7
		rename v267 last_verif_date7
		rename v269 complete_address8
		rename v271 add_num8
		rename v272 pre_direction8
		rename v273 street_name8
		rename v274 street_suffix8
		rename v275 post_direction8
		rename v276 apartment_name8
		rename v277 apartment_number8
		rename v278 city8
		rename v279 state8
		rename v280 zip8
		rename v281 zip_plus8
		rename v285 fips_county8
		rename v286 dwell_type8
		rename v288 effective_date8
		rename v291 orig_file_date8
		rename v292 last_verif_date8
		rename v294 complete_address9
		rename v296 add_num9
		rename v297 pre_direction9
		rename v298 street_name9
		rename v299 street_suffix9
		rename v300 post_direction9
		rename v301 apartment_name9
		rename v302 apartment_number9
		rename v303 city9
		rename v304 state9
		rename v305 zip9
		rename v306 zip_plus9
		rename v310 fips_county9
		rename v311 dwell_type9
		rename v313 effective_date9
		rename v316 orig_file_date9
		rename v317 last_verif_date9
		rename v319 complete_address10
		rename v321 add_num10
		rename v322 pre_direction10
		rename v323 street_name10
		rename v324 street_suffix10
		rename v325 post_direction10
		rename v326 apartment_name10
		rename v327 apartment_number10
		rename v328 city10
		rename v329 state10
		rename v330 zip10
		rename v331 zip_plus10
		rename v335 fips_county10
		rename v336 dwell_type10
		rename v338 effective_date10
		rename v341 orig_file_date10
		rename v342 last_verif_date10
	
		
		if `namefuzzymatch' == 1 {
	
			rename v25 First_Name_1
			rename v27 Last_Name_1
			rename v29 Gender_1
			rename v32 First_Name_2
			rename v34 Last_Name_2
			rename v36 Gender_2
			rename v39 First_Name_3
			rename v41 Last_Name_3
			rename v43 Gender_3
			rename v46 First_Name_4
			rename v48 Last_Name_4
			rename v50 Gender_4
			rename v53 First_Name_5
			rename v55 Last_Name_5
			rename v57 Gender_5
			rename v60 First_Name_6
			rename v62 Last_Name_6
			rename v64 Gender_6
			rename v67 First_Name_7
			rename v69 Last_Name_7
			rename v71 Gender_7
			rename v74 First_Name_8
			rename v76 Last_Name_8
			rename v78 Gender_8
			rename v81 First_Name_9
			rename v83 Last_Name_9
			rename v85 Gender_9
			rename v88 First_Name_10
			rename v90 Last_Name_10
			rename v92 Gender_10
		
		}

		*Label variables:
		label variable PID_ "Infutor ID"
		label variable First_Name_0 "Given Name"
		label variable Last_Name_0 "Surname"
		label variable Gender_0 "Gender (M,F,U)"
		label variable DOB "Date of Birth (YYYYMM)"
		label variable DeceasedCD "Deceased Flag Y means individual is deceased"
		  
		*Drop unnecessary variables:
		drop v*
			
		*Generate ever having lived in specified area:
		qui gen keep = 0
		forvalues x = 1(1)10 {
				
			if `countymatch' == 0 {
			
				qui replace keep = 1 if state`x' == "`extractstate'" 
			
			}
			
			if `countymatch' == 1 {
			
				qui replace keep = 1 if state`x' == "`extractstate'" & fips_county`x' == "`extractcountyfips'"
			
			}
					
		}
					
		*Keep only individuals who ever lived in specified area::
		keep if keep == 1
			
		*Clean some variables:
		forvalues x = 1(1)10 {
			
			tostring state`x', replace 
			destring zip`x', replace force
			destring effective_date`x', replace force
			destring last_verif_date`x', replace force
		
		}
		
		*Manipulating birth date for matching:
		*gen yob_inf = floor(DOB/100)
		*gen mob_inf = DOB - 100 * yob_inf
		
		*Note the state file:
		qui gen statefile = "`state_abbrev`crc''"
		
		*Condense file and save:
		compress
		
		cd "$mydirectory"
		zipsave `state_abbrev`crc''3_cleaned, replace
		
	}
	
	if _N == 0 {
	    
		di "No obs"
		
	}


